﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using NUnit.Framework;
using COFE.Core.Utils;
using COFE.Core;

namespace COFE.IO.FSIEX
{
    public class ExSystemWatcherListener : Component
    {

        #region Constructor

        public ExSystemWatcherListener(FileSystemExDirectoryLister directoryLister, 
            FileSystemExDirectoryController directoryController)
        {            
            Assert.IsNotNull(directoryLister);
            Assert.IsNotNull(directoryController);
            _directoryLister = directoryLister;
            _directoryController = directoryController;

            if (directoryLister.RootPath == null)
                _fileSystemWatcher = new FileSystemWatcherEx(DirectoryInfoEx.DesktopDirectory);
            else _fileSystemWatcher = new FileSystemWatcherEx(directoryLister.RootPath);

            _fileSystemWatcher.OnChanged += new FileSystemEventHandlerEx(
                (o, e) =>
                {
                    _directoryController.NotifyModified(convertToParsePath(e.FullPath),
                        Directory.Exists(e.FullPath), ModifiedAction.Changed);
                }
                );

            _fileSystemWatcher.OnCreated += new FileSystemEventHandlerEx(
                (o, e) =>
                {
                    _directoryController.NotifyModified(convertToParsePath(e.FullPath),
                        Directory.Exists(e.FullPath), ModifiedAction.Created);
                }
                );

            _fileSystemWatcher.OnDeleted += new FileSystemEventHandlerEx(
                (o, e) =>
                {
                    _directoryController.NotifyModified(convertToParsePath(e.FullPath),
                        false, ModifiedAction.Deleted);
                }
                );

            _fileSystemWatcher.OnRenamed += new RenameEventHandlerEx(
                (o, e) =>
                {
                    _directoryController.NotifyModified(convertToParsePath(e.FullPath),
                        Directory.Exists(e.FullPath),
                        ModifiedAction.Renamed,
                        convertToParsePath(e.OldFullPath));
                }
                );
        }


        #endregion


        #region Data

        #endregion


        #region Methods

        private string convertToParsePath(string fullFileSystemPath)
        {
            string parseName = fullFileSystemPath.Replace(_directoryLister.RootPath, "", StringComparison.InvariantCultureIgnoreCase);
            return COFEPath.FormatFullParseName(_directoryLister.ListerKey, parseName);
        }

        #endregion


        #region Data

        FileSystemWatcherEx _fileSystemWatcher;
        FileSystemExDirectoryController _directoryController;
        FileSystemExDirectoryLister _directoryLister;

        #endregion
    }
}
